/*
 * Part of the ACID Project
 * Developed in 2011 by Morgan Taschuk
 */

/*
 * TissueSelectionPanel.java
 *
 * Created on 18-May-2011, 12:11:43
 */
package acid.plugin.gui;

import acid.spring.ExperimentService;
import acid.spring.db.Experiment;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author morgan
 */
public class ExperimentSelectionPanel extends javax.swing.JPanel
{

    private ExperimentService experimentService;

    /** Creates new form TissueSelectionPanel */
    public ExperimentSelectionPanel()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "classpath:/jdbc-services.xml");
        BeanFactory factory = context;
        experimentService = (ExperimentService) factory.getBean(ExperimentService.class);

        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        final List<Experiment> experiments = experimentService.listAllExperiments();
        leftList = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        moveRightButton = new javax.swing.JButton();
        moveLeftButton = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        rightList = new javax.swing.JList();

        jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jList2);

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));

        jLabel1.setText("Select experiments to display (maximum 9):");
        jLabel1.setAlignmentY(0.0F);
        add(jLabel1);

        jPanel1.setAlignmentX(0.0F);
        jPanel1.setAlignmentY(0.0F);
        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        DefaultListModel model = new DefaultListModel();
        for (Experiment  e: experiments )
        {
            model.addElement(e);
        }
        leftList.setModel(model);
        jScrollPane1.setViewportView(leftList);

        jPanel1.add(jScrollPane1);

        jPanel2.setBackground(new java.awt.Color(199, 192, 255));
        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.PAGE_AXIS));

        moveRightButton.setText(">>");
        moveRightButton.setAlignmentX(0.5F);
        moveRightButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveRightButtonActionPerformed(evt);
            }
        });
        jPanel2.add(moveRightButton);

        moveLeftButton.setText("<<");
        moveLeftButton.setAlignmentX(0.5F);
        moveLeftButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveLeftButtonActionPerformed(evt);
            }
        });
        jPanel2.add(moveLeftButton);

        jPanel1.add(jPanel2);

        DefaultListModel model2 = new DefaultListModel();
        rightList.setModel(model2);
        jScrollPane3.setViewportView(rightList);

        jPanel1.add(jScrollPane3);

        add(jPanel1);
    }// </editor-fold>//GEN-END:initComponents

    private void moveRightButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_moveRightButtonActionPerformed
    {//GEN-HEADEREND:event_moveRightButtonActionPerformed
        Experiment experimentToMove = (Experiment) leftList.getSelectedValue();
        if (experimentToMove == null)
        {
            return;
        }
        DefaultListModel leftModel = (DefaultListModel) leftList.getModel();
        DefaultListModel rightModel = (DefaultListModel) rightList.getModel();
        leftModel.removeElement(experimentToMove);
        rightModel.addElement(experimentToMove);
    }//GEN-LAST:event_moveRightButtonActionPerformed

    private void moveLeftButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_moveLeftButtonActionPerformed
    {//GEN-HEADEREND:event_moveLeftButtonActionPerformed
        Experiment experimentToMove = (Experiment) rightList.getSelectedValue();
        if (experimentToMove == null)
        {
            return;
        }
        DefaultListModel leftModel = (DefaultListModel) leftList.getModel();
        DefaultListModel rightModel = (DefaultListModel) rightList.getModel();
        rightModel.removeElement(experimentToMove);
        leftModel.addElement(experimentToMove);
    }//GEN-LAST:event_moveLeftButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList leftList;
    private javax.swing.JButton moveLeftButton;
    private javax.swing.JButton moveRightButton;
    private javax.swing.JList rightList;
    // End of variables declaration//GEN-END:variables

    public List<Experiment> getSelectedExperiments()
    {
        DefaultListModel model = (DefaultListModel) rightList.getModel();
        List<Experiment> tissues = new ArrayList<Experiment>();

        for (int i = 0; i < model.getSize(); i++)
        {
            tissues.add((Experiment) model.getElementAt(i));
        }
        return tissues;
    }
}
